<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('Mnews');
		$this->load->library('pagination');
		$this->lang->load('home', 'vietnamese');
		if (!isset($_SESSION)) {
			session_start();
		}
	}

	function index($page='')
	{
		$limit = 9;

		$config['base_url'] = site_url().'/news/index/';

		$config['first_link'] = 'First';

		$config['last_link'] = 'Last';

		$config['next_link'] = '&gt;&gt;';

		$config['prev_link'] = '&lt;&lt;';

		$config['cur_tag_open'] = '<a href="#" class="current">';

		$config['cur_tag_close'] = '</a>';

		$config['uri_segment'] = 3;

		$config['num_links'] = 2;

		$config['total_rows'] = $this->Mnews->count_all('tbl_news');

		$config['per_page'] = $limit;

		$this->pagination->initialize($config);

    if (empty($page))	$page = 0; //default value
	 $data['page'] = str_replace('&nbsp;', '', $this->pagination->create_links());
		$data['title']='Tin tức';
		$data['main'] = 'newspage';
		$data['result'] = $this->Mnews->get_data($page,$limit);
		$this->load->view('template/index',$data);
	}

	function newstype($typeid,$page='')
	{

		$limit = 9;

		$config['base_url'] = site_url().'/news/newscat/'.$typeid."/";

		$config['first_link'] = 'First';

		$config['last_link'] = 'Last';

		$config['next_link'] = '&gt;&gt;';

		$config['prev_link'] = '&lt;&lt;';

		$config['cur_tag_open'] = '<a href="#" class="current">';

		$config['cur_tag_close'] = '</a>';

		$config['uri_segment'] = 4;

		$config['num_links'] = 5;

		$config['total_rows'] = $this->Mnews->count_all_type($typeid,'tbl_news');


		$config['per_page'] = $limit;

		$this->pagination->initialize($config);

    	if (empty($page))	$page = 0; //default value
	 	$data['page'] = str_replace('&nbsp;', '', $this->pagination->create_links());
		$data['title']='Tin tức';
		$data['main'] = 'newspage_type';
		$data['result'] = $this->Mnews->get_news_type($typeid,$page,$limit);
		$data['result1'] = $this->Mnews->get_nametype($data['result']);
		$data['result2'] = $this->Mnews->get_namecat($data['result']);

		$this->load->view('template/index',$data);
	}

	function newscat($typeid,$page='')
	{
		$limit = 9;

		$config['base_url'] = site_url().'/news/newscat/'.$typeid."/";

		$config['first_link'] = 'First';

		$config['last_link'] = 'Last';

		$config['next_link'] = '&gt;&gt;';

		$config['prev_link'] = '&lt;&lt;';

		$config['cur_tag_open'] = '<a href="#" class="current">';

		$config['cur_tag_close'] = '</a>';

		$config['uri_segment'] = 4;

		$config['num_links'] = 5;

		$config['total_rows'] = $this->Mnews->count_all($typeid,'tbl_news');

		$config['per_page'] = $limit;

		$this->pagination->initialize($config);

    	if (empty($page))	$page = 0; //default value
	 	$data['page'] = str_replace('&nbsp;', '', $this->pagination->create_links());
		$data['title']='Tin tức';
		$data['main'] = 'newspage_type';
		$data['result'] = $this->Mnews->get_news_cat($typeid,$page,$limit);
		//$data['result1'] = $this->Mnews->get_nametype($data['result']);
		$data['result2'] = $this->Mnews->get_namecat($data['result']);
		$this->load->view('template/index',$data);
	}

	function detail($id,$catid){
		$data['title']='Tin tức';
		$data['main'] = 'news_detail';
		$data['result'] = $this->Mnews->info_detail($id,'tbl_news');
		//$data['result1'] = $this->Mnews->get_nametype($data['result']);
		$data['result2'] = $this->Mnews->get_catnew($catid);
		$data['result3'] = $this->Mnews->get_namecat($data['result2']);

		$this->load->view('template/index',$data);
	}

	function search($page=0)
	{
		$limit = 9;
		// lay bien de search
		if($this->input->post()){
			$this->session->set_userdata("search",$this->input->post());
		}
		if($this->session->userdata('search')){
			$_REQUEST = $this->session->userdata("search");
		}
		$keyword = isset($_REQUEST['keyword'])?$_REQUEST['keyword']:$keyword;
		$code = isset($_REQUEST['code'])?$_REQUEST['code']:$code;
		$need = isset($_REQUEST['need'])?$_REQUEST['need']:$need;
		$tower = isset($_REQUEST['tower'])?$_REQUEST['tower']:$tower;
		$Type = isset($_REQUEST['Type'])?$_REQUEST['Type']:$type;
		$Furniture = isset($_REQUEST['Furniture'])?$_REQUEST['Furniture']:$furniture;

		$bedrooms = isset($_REQUEST['bedrooms'])?$_REQUEST['bedrooms']:'';
		$bedrooms2 = isset($_REQUEST['bedrooms2'])?$_REQUEST['bedrooms2']:'';
		$bedrooms=array($bedrooms,$bedrooms2);

		$floors = isset($_REQUEST['floors'])?$_REQUEST['floors']:'';
		$floors2 = isset($_REQUEST['floors2'])?$_REQUEST['floors2']:'';
		$floors=array($floors,$floors2);
		$area = isset($_REQUEST['area'])?$_REQUEST['area']:'';
		$area2 = isset($_REQUEST['area2'])?$_REQUEST['area2']:'';
		$area=array($area,$area2);
		$price_from = isset($_REQUEST['price_from'])?$_REQUEST['price_from']:'';
		$price_to = isset($_REQUEST['price_to'])?$_REQUEST['price_to']:'';
		$price=array($price_from,$price_to);

		$tygia = isset($_REQUEST['tygia'])?$_REQUEST['tygia']:$tygia;
		$pricetype = isset($_REQUEST['price_type'])?$_REQUEST['price_type']:$pricetype;

		$bedrooms3 = is_array($bedrooms) ? implode('-', $bedrooms) : $bedrooms;
		$floors3 = is_array($floors) ? implode('-', $floors) : $floors;
		$area3 = is_array($area) ? implode('-', $area) : $area;
		$price_from3 = is_array($price_from) ? implode('-', $price_from) : $price_from;

		$config['base_url'] = site_url("news/search");
		$config['first_link'] = 'First';
		$config['last_link'] = 'Last';
		$config['next_link'] = '&gt;&gt;';
		$config['prev_link'] = '&lt;&lt;';
		$config['cur_tag_open'] = '<a href="#" class="current">';
		$config['cur_tag_close'] = '</a>';
		$config['uri_segment'] = 3;
		$config['num_links'] = 5;
		$config['total_rows'] = $this->Mnews->count_all_search($code,$keyword,$need,$tower,$Type,$Furniture,$bedrooms,$floors,$area,$price,$tygia,$pricetype);
		$config['per_page'] = $limit;
		$this->pagination->initialize($config);
	    if (empty($page))	$page = 0; //default value
		$data['title']='Tin tức';
		$data['main'] = 'newspage_type';

		$data['result'] = $this->Mnews->newsearch($code,$keyword,$need,$tower,$Type,$Furniture,$bedrooms,$floors,$area,$price,$tygia,$pricetype,$page,$limit);
		$data['result1'] = $this->Mnews->get_nametype($data['result']);
		$data['result2'] = $this->Mnews->get_namecat($data['result']);
		$data['page'] = str_replace('&nbsp;', '', $this->pagination->create_links());
		$this->load->view('template/index',$data);
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */